/*
 Copyright (C) 2010 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.topisv.api.response;

import org.apache.commons.lang.math.NumberUtils;

/**
 * 调用失败结果包装
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">Zhao Fangfang</a>
 * @since 0.2, 2010-9-11
 * @version $Id: ErrorRsp.java 53 2010-10-01 17:06:27Z zhaofang123 $
 */
public class ErrorRsp {
	
	private final String method;
	private final int code;
	private final String msg;
	private final String subCode;
	private final String subMsg;
	
	/**
	 * @param method
	 * @param code
	 * @param msg
	 */
	public ErrorRsp(String method, String code, String msg) {
		this(method, code, msg, null, null);
	}
	
	/**
	 * @param method
	 * @param code
	 * @param msg
	 * @param sub_code
	 * @param sub_msg
	 */
	public ErrorRsp(String method, String code, String msg, String sub_code, String sub_msg) {
		this.method = method;
		this.code = NumberUtils.toInt(code);
		this.msg = msg;
		this.subCode = sub_code;
		this.subMsg = sub_msg;
	}
	
	public String getApiMethod() {
		return method;
	}
	
	public int getCode() {
		return code;
	}
	
	public String getMsg() {
		return msg;
	}
	
	public String getSubCode() {
		return subCode;
	}
	
	public String getSubMsg() {
		return subMsg;
	}
	
	public boolean isErrorIsv() {
		return subCode == null ? false : subCode.startsWith("isv.");
	}
	
	public String toLocalizedMessage() {
		return String.format("%d#%s", getCode(), getSubMsg() == null ? getMsg() : getSubMsg());
	}
	
	@Override
	public String toString() {
		if (subCode != null)
			return String.format("%d#%s(%s#%s)", getCode(), getMsg(), getSubCode(), getSubMsg());
		return String.format("%d#%s", getCode(), getMsg());
	}
}
